package gui.reports.notices;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;

import reports.NoticesReport;
import reports.Report;
import gui.common.*;

/**
 * Controller class for the notices report view.
 */
public class NoticesReportController extends Controller implements INoticesReportController
{

  /**
   * Constructor.
   * 
   * @param view
   *          Reference to the notices report view
   */
  public NoticesReportController(IView view)
  {
    super(view);

    construct();
  }

  //
  // Controller overrides
  //

  /**
   * Returns a reference to the view for this controller.
   * 
   * {@pre None}
   * 
   * {@post Returns a reference to the view for this controller.}
   */
  @Override
  protected INoticesReportView getView()
  {
    return (INoticesReportView) super.getView();
  }

  /**
   * Sets the enable/disable state of all components in the controller's view. A component should
   * be enabled only if the user is currently allowed to interact with that component.
   * 
   * {@pre None}
   * 
   * {@post The enable/disable state of all components in the controller's view have been set
   * appropriately.}
   */
  @Override
  protected void enableComponents()
  {
    getView().enableOK(true);
  }

  /**
   * Loads data into the controller's view.
   * 
   * {@pre None}
   * 
   * {@post The controller has loaded data into its view}
   */
  @Override
  protected void loadValues()
  {
    getView().setFormat(FileFormat.PDF);
  }

  //
  // IExpiredReportController overrides
  //

  /**
   * This method is called when any of the fields in the notices report view is changed by the
   * user.
   */
  @Override
  public void valuesChanged()
  {
    enableComponents();
  }

  /**
   * This method is called when the user clicks the "OK" button in the notices report view.
   */
  @Override
  public void display()
  {
    final Report report = new NoticesReport();

    report.generateReport(new ReportCallback(getView())
    {
      @Override
      public void success()
      {
        try
        {
          File file;
          if (getView().getFormat() == FileFormat.HTML)
            file = report.toHtml();
          else
            file = report.toPdf();

          Desktop.getDesktop().open(file);
        }
        catch (IOException e)
        {
          failure(e);
        }
      }

      @Override
      public void finished()
      {}
    });
  }
}
